<template>
  <div class="common-layout">
    <el-container>
      <index-header></index-header>
      <el-container class="container">
        <!-- @getList="receiveslide" -->
        <show-slide></show-slide>
        <el-header>
          <el-input class="el-input__inner" v-model="searchKey"  placeholder="输入关键字搜索" > <template #prepend>搜索</template>
            <template #append>
              <el-button @click="searchBook">
                <el-icon>
                  <Search />
                </el-icon>
              </el-button></template>
          </el-input>
        </el-header>
        <el-main class="main">
          <div class="recommend">
            <div class="title-1"><span class="sp-title">推荐</span>
              <span class="login-l sp-more"><router-link to="/more/recommend">更多>>></router-link></span>
            </div>
            <div v-for="item in bookByRecommendList" :key="item.bookId" class="items">
              <div style="width:176px;height:244px;">
                <img style="width:176px;height:244px;" :src="item.bookpic" />
              </div>
              <div style="width:176px;height:26px;">
                <p class="hidden-text">
                  {{ item.bookname }}
                </p>
              </div>
              <div style="width:176px;background:white;margin-top:4px;">
                <p style="line-height:0;padding-right:5px;"><span class="price">￥{{ item.price }}</span>
                  <a class="detail"  @click="detail(item.bookId)">商品详情</a>
                </p>
              </div>
            </div>
          </div>

          <div class="list">
            <div class="title-1"><span class="sp-title">图书列表</span>
              <span class="login-l sp-more"><router-link to="/more/list">更多>>></router-link></span>
            </div>
            <div v-for="item in bookList" :key="item.bookId" class="items">
              <div style="width:176px;height:244px;">
                <img style="width:176px;height:244px;" :src="item.bookpic" />
              </div>
              <div style="width:176px;height:26px;">
                <p class="hidden-text">
                  {{ item.bookname }}
                </p>
              </div>
              <div style="width:176px;background:white;margin-top:4px;">
                <p style="line-height:0;padding-right:5px;"><span class="price">￥{{ item.price }}</span>
                  <a class="detail" @click="detail(item.bookId)">商品详情</a>
                </p>
              </div>
            </div>
          </div>
        </el-main>

      </el-container>

      <el-footer class="head">


          <ul class="linkes">
            <li><a href="javascript:;">关于我们</a></li>
            <li><a href="javascript:;">服务条款</a></li>
            <li><a href="javascript:;">加入我们</a></li>
            <li><a href="javascript:;">联系我们</a></li>
            <li><a href="javascript:;">隐私声明</a></li>
            <li><a href="javascript:;">广告服务</a></li>
          </ul>
   
      </el-footer>
    </el-container>
  </div>
</template>

<script>
import showSlide from "../components/showSlide.vue"
import { toRefs, reactive } from '@vue/reactivity'
import { bookShowApi } from "../utils/clientApi"
import { useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import IndexHeader from '@/components/indexHeader.vue'
export default {
  name: 'HomeView',
  components: { showSlide, IndexHeader },
  setup(props) {
    const router = useRouter()
   
    const data = reactive({
      bookByRecommendList: [],
      bookList: [],
      searchKey:""
    })
    const getBookList = () => {
      bookShowApi().then(res => {
        if (res.status === 200) {
          data.bookByRecommendList = res.data.bookByRecommend.list
          data.bookList = res.data.bookList.list
          ElMessage.success(res.msg)
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
    getBookList()
    //   详情页
    const detail = (bookId) => {
      router.push({
        path: "/goodsdetail",
        query: { bookId: bookId }
      })
    }

    // 首页搜索
    const searchBook=()=>{
        const keyword = data.searchKey
        // console.log(keyword);
        if(keyword !== null){
          // console.log(keyword);
          router.push({
            path:'/search',
            query: { keyword: keyword  }
          })
        }

    }
    return {
      ...toRefs(data),
      getBookList,
      detail,
      searchBook
    }
  }


}
</script>
<style scoped>
.main {
  width: 1200px;
  height: auto;
  /* background: #0098f7;*/
  margin: 0 auto;
}

.container {
  width: 1200px;
  margin: 0 auto;
  margin-top: 5px;
  padding-top: 5px;
  height: auto;
  background: #ffff;
}

.el-input__inner {
  width: 500px;
}

/*商品列表
*/
.items {
  float: left;
  width: 16%;
  height: 310px;
  margin-left: 40px;
  margin-top: 20px;
  margin-bottom: 15px;
  box-shadow: 0px 1px 2px 0px rgb(200, 200, 200);

}

.title-1 {
  width: 100%;
  height: 42px;
  background: #F5F5F5;
  margin: 0 auto;
  padding-left: 11px;
  padding-top: 8px;
  padding-right: 5px;
}

.sp-title {
  float: left;
  font-size: 25px;
}

.sp-more {
  float: right;
  margin-top: 18px;
  margin-right: 40px;
  font-size: 13px;

}

.sp-more>a:hover {
  color: #0098f7;
}

.recommend {
  margin: 0 auto;
  width: 1100px;
  height: 742px;
  overflow: hidden;

}

.list {
  margin: 0 auto;
  width: 1100px;
  height: 1080px;
  overflow: hidden;
}

.hidden-text {
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  /*禁止换行显示*/
  white-space: nowrap;
  font-size: 16px;
  float: left;
  margin: 3px 0 0 5px;
}

.price {
  position: relative;
  left: -64px;
  font-size: 18px;
  color: red;
  margin: 3px 0 0 5px;
  line-height: 0;
}

.detail {
  line-height: 0px;
  float: right;
  color: #0098f7;
  font-size: 12px;
}

a:hover {
  cursor: pointer;
}


/*页尾*/
a{
	text-decoration: none;
}
.linkes {
	list-style: none;
	margin: 20px 90px 5px 0;
	padding: 0;
	padding-bottom: 8px;
	height: 18px;
}
 
.linkes>li {
	display: inline-block;
}
 
/*li后面的li添加*/
.linkes>li+li::before{
	content: "|";
    margin:0 8px;
}
 

</style>